"use strict";

var app = angular.module('linkManagementApp', ['angularUtils.directives.dirPagination']);

app.controller('linkManagementController', function($scope, $http) {
  var $ = jQuery;
  $scope.nips = [];
  $scope.nips_count = 0;
  $scope.nips_per_page = Drupal.settings.link_management.nips_per_page;
  $scope.nips_selected = [];

  var get_url = '/cpanel/link-management/get';
  var post_url = '/cpanel/link-management/post';
  var update_url = '/cpanel/link-management/update';
  var delete_url = '/cpanel/link-management/delete';
  var token = Drupal.settings.link_management.token;
  var current_page = 0;

  get_results_page(1);

  $scope.page_changed = function(new_page) {
    get_results_page(new_page);
  };

  $scope.delete_nips = function() {
    $http({
      method: 'POST',
      url: delete_url,
      data: jQuery.param({nips_selected: $scope.nips_selected, link_management_token: token}),
      headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    })
      .success(function (data) {
        reset();
      });
  }

  $scope.delete_nip = function(nip) {
    $scope.nips_selected = [];
    $('.checkbox-nip').prop('checked', false);
    $scope.nips_selected.push(nip);
  }

  $scope.bulk_actions_enabled = function() {
    return $scope.nips_selected.length > 0
  }

  /**
   * get nips from remote url
   */
  function get_results_page(page_number) {
    current_page = page_number;
    if(page_number > 0) {
      page_number--; // drupal start page number from 0
    }

    $http.get(get_url + '?page=' + page_number + '&link_management_token=' + token)
      .success(function (data) {
        $scope.nips = data['nips'];
        $scope.nips_count = data['count'];
      });
  }

  function reset() {
    get_results_page(current_page);
    $scope.nips_selected = [];
    $('.checkbox-nip').prop('checked', false);
    $('.modal').modal('hide');
  }

});

/**
 * handling checkboxes since there's no easy way to do that in angular
 */
jQuery(document).ready(function($) {
  var $scope = angular.element('.cpanel-link-management').scope(); // get $scope

  $('.checkbox-all', '.cpanel-link-management').change(function () {
    $scope.$apply(function() {
      $scope.nips_selected = [];
    });
    $('.checkbox-nip').prop('checked', $(this).prop('checked')).trigger('change');
  });

  $('.cpanel-link-management').on('change', '.checkbox-nip', function() {
    var $this = $(this);
    var val = $this.val();
    var nips_selected = $scope.nips_selected;

    if($this.prop('checked')) {
      $.each($scope.nips, function(index, nip) {
        if(val == nip.nid) {
          nips_selected.push($scope.nips[index]);
          return false;
        }
      });
    }
    else {
      // find index
      $.each(nips_selected, function(index, nip) {
        if(nip.nid == val) {
          nips_selected.splice(index, 1);
          return false;
        }
      });
    }

    $scope.$apply(function() {
      $scope.nips_selected = nips_selected;
    });
  });
});
